package com.a3dweijing.wetouch2.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.a3dweijing.wetouch2.Activity.VideoPlayActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.PartitionRecItemAdapter;
import com.a3dweijing.wetouch2.bean.ExpoonVideo;
import com.a3dweijing.wetouch2.bean.GetTVVideo;
import com.a3dweijing.wetouch2.bean.VideoType;
import com.a3dweijing.wetouch2.callback.PartitionOnclick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.recyclerviewdecoration.RecycleViewDivider;
import com.google.gson.Gson;

/**
 * Created by yxd on 2017/11/29.
 */

public class TVVideoHotFragment extends Fragment implements PartitionOnclick {
    private Handler handler;
    private RecyclerView recyclerView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static TVVideoHotFragment newinstance() {
        Bundle bundle = new Bundle();
        bundle.putString("name", "tv_video");
        TVVideoHotFragment tvVideoFragment = new TVVideoHotFragment();
        tvVideoFragment.setArguments(bundle);
        return tvVideoFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tv_videohot, container, false);
        recyclerView = (RecyclerView) v.findViewById(R.id.tv_videohot_rec);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.addItemDecoration(new RecycleViewDivider(10, Color.WHITE));
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 100:
                        GetTVVideo video = new Gson().fromJson(msg.obj.toString(), GetTVVideo.class);
                        if (video != null && video.isFlag() && video.getOriginalVideoList() != null && video.getOriginalVideoList().size() > 0) {
                            recyclerView.setAdapter(new PartitionRecItemAdapter(video.getOriginalVideoList(),TVVideoHotFragment.this));
                        }
                    case 404:
//                        Toast.makeText(getBaseContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        };
        new VideoHotThread("1").start();
        return v;
    }

    @Override
    public void typeclick(int position, VideoType type) {

    }

    @Override
    public void videoclick(int position, Object o) {
        Intent intent=new Intent(getContext(), VideoPlayActivity.class);
        ExpoonVideo video= (ExpoonVideo) o;
        intent.putExtra("video",video);
        startActivity(intent);
    }


    private class VideoHotThread extends Thread {
        String pageNo;

        public VideoHotThread(String sc) {
            this.pageNo = sc;
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetTVVideoHot(pageNo);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }
}
